package com.library.servlet.admin;

import com.library.dao.AdminDao;
import com.library.entity.Admin;
import com.library.entity.po.BookRankPO;
import com.library.entity.po.StudentRankPO;
import com.library.service.admin.AdminService;
import com.library.service.admin.BookService;
import com.library.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@WebServlet("/admin")
public class AdminServlet extends BaseServlet {

    private final AdminService adminService = new AdminService();
    private final BookService bookService = new BookService();


    /**
     * 管理员登录
     *
     * @param request
     * @param response
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        boolean result = adminService.login(username, password, request);
        if (result) {
            // 成功登录

        }

        // 登录失败
    }

    /**
     * 获取学生和图书排行榜，重定向至首页
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void adminIndex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Set<BookRankPO> bookRank = bookService.getBookRank();
        request.setAttribute("bookRank", bookRank);
        Set<StudentRankPO> studentRank = bookService.getStudentRank();
        request.setAttribute("studentRank", studentRank);
        request.getRequestDispatcher("/WEB-INF/admin/adminIndex.jsp").forward(request, response);
    }


}
